package com.bos.controller.json;

import com.bos.dao.PeriodeDAO;
import com.bos.dao.TahunDAO;
import com.bos.dao.TriwulanDAO;
import com.bos.domain.Periode;
import com.bos.domain.Tahun;
import com.bos.domain.Triwulan;
import com.bos.editor.PeriodeEditor;
import com.bos.editor.TahunEditor;
import com.bos.editor.TriwulanEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class PeriodeJsonController {

    private PeriodeDAO periodeDAO;
    private TahunDAO tahunDAO;
    private TriwulanDAO triwulanDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setTahunDAO(TahunDAO tahunDAO) {
        this.tahunDAO = tahunDAO;
    }

    @Autowired
    public void setTriwulanDAO(TriwulanDAO triwulanDAO) {
        this.triwulanDAO = triwulanDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/periode.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Periode> index() {
        try {
            return periodeDAO.getAll();
        } catch (DataAccessException e) {
            return new ArrayList<Periode>();
        }
    }

    @RequestMapping(value = "/periode/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Periode show(@PathVariable("id") Periode periode) {
        return periode;
    }

    @RequestMapping(value = "/periode/tahun/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Periode> getByKabupaten(@PathVariable("id") Tahun tahun) {
        List<Periode> periodes = new ArrayList<Periode>();
        try {
            periodes = periodeDAO.getByTahun(tahun);
        } catch (DataAccessException e) {
        } finally {
            return periodes;
        }
    }

    @RequestMapping(value = "/periode.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody Periode periode) {
        if (periode == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            periodeDAO.save(periode);
            result.put("success", true);
            result.put("periode", periode);
            result.put("message", "Sukses Save Periode " + periode);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("periode", periode);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Periode " + periode);
        }
        return result;
    }

    @RequestMapping(value = "/periode/{id}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("id") Periode periode) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            periodeDAO.delete(periode);
            result.put("success", true);
            result.put("message", "Sukses Delete Periode " + periode);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Delete Periode " + periode);
        }
        return result;
    }

    @RequestMapping(value = "/periode.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody Periode periode) {
        if (periode == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            periodeDAO.merge(periode);
            result.put("success", true);
            result.put("periode", periode);
            result.put("message", "Sukses Update Periode " + periode);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("periode", periode);
            result.put("message", "Gagal Update Periode " + periode);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Tahun.class, new TahunEditor(tahunDAO));
        binder.registerCustomEditor(Triwulan.class, new TriwulanEditor(triwulanDAO));
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
    }

}
